﻿using Assessment.BLL;
using Assessment.Common;
using Assessment.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

public partial class Admin_Gauge_GaugeEdit : BasePage
{
    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    #endregion

    T_Gauge model = new T_Gauge();
    T_GaugeBLL bll = new T_GaugeBLL();
    T_GaugeTypeBLL type_bll = new T_GaugeTypeBLL();
    T_GaugeTestBLL test_bll = new T_GaugeTestBLL();
    T_GaugeTestItemBLL item_bll = new T_GaugeTestItemBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");
        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            BindDropDownList();

            if (Action == "Modify" || Action == "Detail")
            {
                this.panel_Edit.Visible = true;
                getDate(Id);
            }
            else
            {
                this.panel_Edit.Visible = false;
            }
            if (Action == "Detail")
            {
                this.ddl_GaugeType.Enabled = false;
                this.txt_Title.Enabled = false;
                this.ch_Valid.Enabled = false;

                this.btn_Modify.Visible = true;
                this.btn_Save.Visible = false;
            }
            else if (Action == "Add" || Action == "Modify")
            {
                this.ddl_GaugeType.Enabled = true;
                this.txt_Title.Enabled = true;
                this.ch_Valid.Enabled = true;

                this.btn_Modify.Visible = false;
                this.btn_Save.Visible = true;
            }
        }
    }
    #endregion

    #region 绑定下拉框
    private void BindDropDownList()
    {
        List<T_GaugeType> list = type_bll.GetModelList("Valid=1");
        this.ddl_GaugeType.DataTextField = "Title";
        this.ddl_GaugeType.DataValueField = "GaugeTypeId";
        this.ddl_GaugeType.DataSource = list;
        this.ddl_GaugeType.DataBind();
        this.ddl_GaugeType.Items.Insert(0, new ListItem("----请选择----", "0"));
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(int id)
    {
        model = bll.GetModel(id);
        this.ddl_GaugeType.SelectedValue = model.GaugeTypeId.ToString();
        this.txt_Title.Text = model.Title;
        this.ch_Valid.Checked = model.Valid == 1;
        this.hf_GaugeId.Value = id.ToString();
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (Action == "Modify")
        {
            model = bll.GetModel(Id);
        }
        else if (Action == "Add")
        {
            model = new T_Gauge();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }
        model.GaugeTypeId = Convert.ToInt32(this.ddl_GaugeType.SelectedValue);
        model.Title = this.txt_Title.Text.Trim();
        model.Valid = this.ch_Valid.Checked ? 1 : 0;

        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "Add")
            {
                //保存
                Id = bll.Add(model);
            }
            base.Redirect("操作成功！", "GaugeEdit.aspx?Action=Detail&Id=" + Id.ToString());
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("GaugeEdit.aspx?Action=Modify&Id=" + Id);
    }
    #endregion



    #region 题目编辑，Ajax操作
    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="JsonModel">主表</param>
    /// <param name="JsonList">从表</param>
    /// <returns></returns>
    [WebMethod]
    public static string SaveGaugeTestDate(string JsonModel, string JsonList)
    {
        T_GaugeTestBLL bll = new T_GaugeTestBLL();
        T_GaugeTestItemBLL item_bll = new T_GaugeTestItemBLL();

        T_GaugeTest Model = JsonConvert.DeserializeObject<T_GaugeTest>(JsonModel);
        List<T_GaugeTestItem> List = JsonConvert.DeserializeObject<List<T_GaugeTestItem>>(JsonList);

        try
        {
            if (Model.GaugeTestId > 0)
            {
                T_GaugeTest uModel = bll.GetModel(Model.GaugeTestId);
                uModel.Title = Model.Title;
                uModel.TestType = Model.TestType;
                uModel.UpdateDate = BasePage.GetNowTimeString();
                uModel.UpdateUserId = BasePage.CurrentSession2.UserID;
                uModel.GridValue = Model.GridValue;
                uModel.GridParams = Model.GridParams;

                bll.Update(uModel);
            }
            else
            {
                if (Model == null)
                {
                    return "null";
                }
                Model.CreateDate = BasePage.GetNowTimeString();
                Model.CreateUserId = BasePage.CurrentSession2.UserID;
                //Model.GaugeTestId
                Model.IsLeef = 1;
                Model.OrderNo = "";
                Model.ParentId = 0;
                //Model.TestType=
                //Model.Title
                Model.UpdateDate = BasePage.GetNowTimeString();
                Model.UpdateUserId = BasePage.CurrentSession2.UserID;
                Model.Valid = 1;
                Model.GaugeTestId = bll.Add(Model);
            }

            //删除项
            string GaugeTestItemIds = "";
            foreach (T_GaugeTestItem item in List)
            {
                if (GaugeTestItemIds != "")
                {
                    GaugeTestItemIds += ",";
                }
                GaugeTestItemIds += item.GaugeTestItemId;
            }
            if (GaugeTestItemIds.Length > 0)
            {
                string strDeleteSQL = " select GaugeTestItemId from T_GaugeTestItem where GaugeTestId=" + Model.GaugeTestId + " and GaugeTestItemId not in (" + GaugeTestItemIds + ")";
                item_bll.DeleteList(strDeleteSQL);
            }

            foreach (T_GaugeTestItem item in List)
            {
                if (item.GaugeTestItemId > 0)
                {
                    T_GaugeTestItem uModel = item_bll.GetModel(item.GaugeTestItemId);
                    uModel.Value = item.Value;
                    uModel.Score = item.Score;
                    uModel.ItemTypeNo = item.ItemTypeNo;
                    uModel.UpdateDate = BasePage.GetNowTimeString();
                    uModel.UpdateUserId = BasePage.CurrentSession2.UserID;

                    //修改
                    item_bll.Update(uModel);
                }
                else
                {
                    item.GaugeTestId = Model.GaugeTestId;
                    item.UpdateDate = BasePage.GetNowTimeString();
                    item.UpdateUserId = BasePage.CurrentSession2.UserID;
                    item.CreateUserDate = BasePage.GetNowTimeString();
                    item.CreateUserId = BasePage.CurrentSession2.UserID;
                    item.Formula = "";
                    item.GaugeTestId = Model.GaugeTestId;
                    //item.GaugeTestItemId
                    //item.ItemTypeNo=
                    //item.Score
                    //item.Selected = 1;
                    item.UpdateDate = BasePage.GetNowTimeString();
                    item.UpdateUserId = BasePage.CurrentSession2.UserID;
                    item.Valid = 1;
                    //item.Value

                    //新增
                    item_bll.Add(item);
                }
            }
            return Model.GaugeTestId.ToString();
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

    /*
     * 题目的项的操作
     */
    [WebMethod]
    public static List<T_GaugeTest> GetGaugeTestList(int GaugeId)
    {
        List<T_GaugeTest> list = new T_GaugeTestBLL().GetModelList("Valid=1 and GaugeId=" + GaugeId);

        return list;
    }

    [WebMethod]
    public static T_GaugeTest GetGaugeTestData(int Id)
    {
        T_GaugeTest data = new T_GaugeTestBLL().GetModel(Id);
        //data.TestType
        return data;
    }

    [WebMethod]
    public static int DelGaugeTestData(int GaugeTestId)
    {
        return (new T_GaugeTestBLL().Delete(GaugeTestId) ? 1 : -1);
    }

    [WebMethod]
    public static List<T_GaugeTestItem> GetGaugeTestItemList(int GaugeTestId)
    {
        List<T_GaugeTestItem> data = new T_GaugeTestItemBLL().GetModelList("GaugeTestId=" + GaugeTestId);
        return data;
    }

    [WebMethod]
    public static string SaveGaugeTestItemList(int TestId, string List)
    {
        try
        {
            List<T_GaugeTestItem> itemList = JsonConvert.DeserializeObject<List<T_GaugeTestItem>>(List);
            T_GaugeTestItemBLL itembll = new T_GaugeTestItemBLL();

            string GaugeTestItemIds = "";
            foreach (T_GaugeTestItem model in itemList)
            {
                if (GaugeTestItemIds != "")
                {
                    GaugeTestItemIds += ",";
                }
                GaugeTestItemIds += model.GaugeTestItemId;
            }


            //删除项
            string strDeleteSQL = " select GaugeTestItemId from T_GaugeTestItem where GaugeTestId=" + TestId + " and GaugeTestItemId not in (" + GaugeTestItemIds + ")";
            itembll.DeleteList(strDeleteSQL);

            foreach (T_GaugeTestItem model in itemList)
            {
                if (model.GaugeTestItemId > 0)
                {
                    T_GaugeTestItem sModel = itembll.GetModel(model.GaugeTestItemId);
                    sModel.Value = model.Value;
                    sModel.Score = model.Score;
                    sModel.ItemTypeNo = model.ItemTypeNo;
                    //修改
                    itembll.Update(sModel);
                }
                else
                {
                    model.GaugeTestId = TestId;
                    //新增
                    itembll.Add(model);
                }
            }
            return "OK";
        }
        catch (Exception ex)
        {
            return "操作出现异常！      EX:" + ex;
        }
    }
    #endregion
}